package com.andova.empty.app;

import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.andova.empty.api.IEmptyApplicationOption;
import com.andova.empty.api.IEmptyPlugin;
import com.andova.empty.widget.EmptyLinearLayout;
import com.andova.empty.widget.EmptyRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * Created by Administrator on 2018-02-08.
 *
 * @author kzaxil
 * @since 1.0.0
 */
@Deprecated
class EmptyAppCompatActivity extends AppCompatActivity {
    private List<IEmptyPlugin> mEmptyLayoutList = new ArrayList<>(1);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        if (!available() || parent == null) return super.onCreateView(parent, name, context, attrs);
        if (validIdList() == null && mEmptyLayoutList.size() > 0)
            return super.onCreateView(parent, name, context, attrs);
        if (parent instanceof IEmptyPlugin && mEmptyLayoutList.contains(parent))
            return super.onCreateView(parent, name, context, attrs);
        boolean canNext = false;
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            if (validIdList() == null || validIdList().size() == 0) {
                canNext = true;
                break;
            }
            if (!attrs.getAttributeName(i).equals("id")) continue;
            canNext = true;
            int id = attrs.getAttributeResourceValue(i, 0);
            if (!validIdList().contains(id))
                return super.onCreateView(parent, name, context, attrs);
        }
        if (!canNext) return super.onCreateView(parent, name, context, attrs);
        name = name.lastIndexOf(".") == -1 ? name : name.substring(name.lastIndexOf("."));
        switch (name) {
            case "RelativeLayout":
                EmptyRelativeLayout erlChild = new EmptyRelativeLayout(layoutOption(), this, attrs);
                mEmptyLayoutList.add(erlChild);
                return erlChild;
            case "LinearLayout":
                EmptyLinearLayout ellChild = new EmptyLinearLayout(layoutOption(), this, attrs);
                mEmptyLayoutList.add(ellChild);
                return ellChild;
        }
        return super.onCreateView(parent, name, context, attrs);
    }

    public void showEmptyView() {
        for (IEmptyPlugin plugin : mEmptyLayoutList) {
            plugin.showEmptyView();
        }
    }

    public void showEmptyView(View emptyView) {
        if (emptyView instanceof IEmptyPlugin) {
            if (!mEmptyLayoutList.contains(emptyView)) return;
            ((IEmptyPlugin) emptyView).showEmptyView();
        }
    }

    public void showEmptyView(ViewParent emptyView) {
        if (emptyView instanceof IEmptyPlugin) {
            if (!mEmptyLayoutList.contains(emptyView)) return;
            ((IEmptyPlugin) emptyView).showEmptyView();
        }
    }

    public void showErrorView() {
        for (IEmptyPlugin plugin : mEmptyLayoutList) {
            plugin.showErrorView();
        }
    }

    public void showErrorView(View errorView) {
        if (errorView instanceof IEmptyPlugin) {
            if (!mEmptyLayoutList.contains(errorView)) return;
            ((IEmptyPlugin) errorView).showErrorView();
        }
    }

    public void showErrorView(ViewParent errorView) {
        if (errorView instanceof IEmptyPlugin) {
            if (!mEmptyLayoutList.contains(errorView)) return;
            ((IEmptyPlugin) errorView).showErrorView();
        }
    }

    public void showLoadingView() {
        for (IEmptyPlugin plugin : mEmptyLayoutList) {
            plugin.showLoadingView();
        }
    }

    public void showLoadingView(View loadingView) {
        if (loadingView instanceof IEmptyPlugin) {
            if (!mEmptyLayoutList.contains(loadingView)) return;
            ((IEmptyPlugin) loadingView).showLoadingView();
        }
    }

    public void showLoadingView(ViewParent loadingView) {
        if (loadingView instanceof IEmptyPlugin) {
            if (!mEmptyLayoutList.contains(loadingView)) return;
            ((IEmptyPlugin) loadingView).showLoadingView();
        }
    }

    public void showContentView() {
        for (IEmptyPlugin plugin : mEmptyLayoutList) {
            plugin.showContentView();
        }
    }

    public void showContentView(View contentView) {
        if (contentView instanceof IEmptyPlugin) {
            if (!mEmptyLayoutList.contains(contentView)) return;
            ((IEmptyPlugin) contentView).showContentView();
        }
    }

    public void showContentView(ViewParent contentView) {
        if (contentView instanceof IEmptyPlugin) {
            if (!mEmptyLayoutList.contains(contentView)) return;
            ((IEmptyPlugin) contentView).showContentView();
        }
    }

    public IEmptyApplicationOption layoutOption() {
        return null;
    }

    public View getRootView() {
        return ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
    }

    public List<Integer> validIdList() {
        return null;
    }

    public boolean available() {
        return true;
    }
}
